﻿using Magicodes.ExporterAndImporter.Core.Models;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.RecurringJobs;
using NP.BPMReportPlatform.SeminarManage.Dtos;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.SeminarManage
{
    [Authorize]
    [ApiController]
    [Route("api/gc")]
    public class SeminarManageController : BPMReportPlatformController
    {
        private readonly ISeminarManageService _seminarManageService;
        private readonly ISeminarManageJob _job;

        public SeminarManageController(ISeminarManageService seminarManageService, ISeminarManageJob job)
        {
            this._seminarManageService = seminarManageService;
            this._job = job;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SeminarManage/GetAllAsync")]
        public async Task<PagedResultDto<SeminarManageOutput>> GetAllAsync([FromBody] SeminarManageInput input)
        {
            return await _seminarManageService.GetAllAsync(input);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SeminarManage/ExportAsync")]
        public async Task<FileResult> ExportAsync([FromBody] SeminarManageInput input)
        {
            var rst = await _seminarManageService.Export1Async(input);
            string downloadFileName = "TU推广会报表" + DateTime.Now.ToString("yyyyMMddHHmmss");
            rst.Close();
            return File(rst.ToArray(), "application/octet-stream", $"{downloadFileName}.xlsx");
        }

        /// <summary>
        /// 区域生态伙伴年度合作共识会
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SeminarManage/GetAreaAllAsync")]
        public async Task<PagedResultDto<AreaSeminarManageOutput>> GetAreaAllAsync([FromBody] SeminarManageInput input)
        {
            return await _seminarManageService.GetAreaAllAsync(input);
        }

        [HttpPost]
        [Route("SeminarManage/AreaExport")]
        public async Task<XlsxFileResult<AreaSeminarManageOutput>> AreaExport(SeminarManageInput input)
        {
            var rst = await _seminarManageService.AreaExport(input);
            return new XlsxFileResult<AreaSeminarManageOutput>(rst, "区域生态伙伴年度合作共识会");
        }

        [HttpPost]
        [Route("SeminarManage/GetAllAsyncFor2024")]
        public async Task<PagedResultDto<SeminarManageFor2024Output>> GetAllAsyncFor2024([FromBody] SeminarManageInput input)
        {
            return await _seminarManageService.GetAllAsyncFor2024(input);
        }

        [HttpPost]
        [Route("SeminarManage/Export2024Async")]
        public async Task<FileResult> Export2024Async([FromBody] SeminarManageInput input)
        {
            var rst = await _seminarManageService.ExportFor2024Async(input);
            string downloadFileName = "TU一般推广会报表" + DateTime.Now.ToString("yyyyMMddHHmmss");
            rst.Close();
            return File(rst.ToArray(), "application/octet-stream", $"{downloadFileName}.xlsx");
        }

        /// <summary>
        /// 测试邮件催办
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("SeminarManage/Test")]
        public async Task Test()
        {
            var rst = _job.SyncSeminarManage();
        }

        /// <summary>
        /// 作废单据
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("SeminarManage/Cancel")]
        public async Task<bool> Cancel([FromQuery] string number)
        {
            return await _seminarManageService.Cancel(number);
        }
    }
}
